package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.entity.AppUser;
import com.sixmac.entity.Image;
import com.sixmac.entity.Malfunction;
import com.sixmac.entity.WxUser;
import com.sixmac.service.*;
import com.sixmac.utils.WebUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/30 0030.
 */
@Controller
@RequestMapping(value = "backend/malfunction")
public class MalfunctionController extends CommonController {

    @Autowired
    private MalfunctionService malfunctionService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private OperaService operaService;

    @Autowired
    private ImageService imageService;

    @RequestMapping("/index")
    public String index() {
        return "backend/故障报修列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer id,
                     String mobile,
                     Integer type,
                     Integer areaId,
                     Integer status,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<Malfunction> page = malfunctionService.pageByParams(id, mobile, type, areaId, status, pageNum, length);

        AppUser appUser = null;
        WxUser wxUser = null;
        for (Malfunction malfunction : page.getContent()) {
            switch (malfunction.getType()) {
                case 1:
                    // APP端
                    appUser = appUserService.getById(malfunction.getUserId());
                    malfunction.setUserName(null != appUser ? appUser.getMobile() : "");
                    break;
                case 2:
                    // 微信端
                    wxUser = wxUserService.getById(malfunction.getUserId());
                    malfunction.setUserName(null != wxUser ? wxUser.getNickname() : "");
                    break;
            }
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/show")
    public String info(ModelMap model, Integer id) {
        List<String> list = new ArrayList<String>();
        Malfunction malfunction = malfunctionService.getById(id);

        if (malfunction.getType() == 1) {
            // APP端
            AppUser appUser = appUserService.getById(malfunction.getUserId());
            malfunction.setUserName(null != appUser ? appUser.getMobile() : "");
        } else {
            // 微信端
            WxUser wxUser = wxUserService.getById(malfunction.getUserId());
            malfunction.setUserName(null != wxUser ? wxUser.getNickname() : "");
        }

        List<Image> imageList = imageService.findList(id, Constant.IMAGE_TYPE_001);

        for (Image image : imageList) {
            list.add(image.getPath());
        }

        model.addAttribute("malfunction", malfunction);

        model.addAttribute("imageList", JSONArray.fromObject(list));

        return "backend/故障报修详情";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Integer updateStatus(HttpServletRequest request, Integer id) {
        try {
            Malfunction malfunction = malfunctionService.getById(id);
            malfunction.setStatus(Constant.EVENT_STATUS_001);
            malfunctionService.update(malfunction);

            operaService.addOperaInfo(request, "确认开始处理报修 " + malfunction.getId());

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
